package btb.ui;


import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

import de.matthiasmann.twl.Alignment;
import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.GUI;
import de.matthiasmann.twl.Label;
import de.matthiasmann.twl.TextArea;
import de.matthiasmann.twl.Widget;
import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;
import de.matthiasmann.twl.textarea.SimpleTextAreaModel;
import de.matthiasmann.twl.theme.ThemeManager;

import btb.util.CGuiUtils;


public class CLogoutGui extends Widget
{
	public boolean quit;
	public String logouttext;
	
	private static final int DEFAULT_WINDOW_WIDTH = 400;
	private static final int DEFAULT_WINDOW_HEIGHT = 300;
	
	private final Label lblTitle;
	private final SimpleTextAreaModel txtLogoutModel;
	private final TextArea txtLogout;
	private final Button btnLogout;
	private final Button btnExit;
	
	protected GUI gui;
	protected ThemeManager theme;
	
	
	public CLogoutGui( String currentUser )
	{
		logouttext = "You are logged on as " + currentUser + ". Do you want to log off?";
		
		lblTitle = new Label();
		lblTitle.setText( "Beat the Bard" );
		lblTitle.setTheme( "gamelabel" );
		add( lblTitle );
		
		//txtLogonInfo = new TextFrame( loginhtml, 2 );
		txtLogoutModel = new SimpleTextAreaModel();
		txtLogout = new TextArea( txtLogoutModel );
		txtLogoutModel.setText( logouttext );
		txtLogout.setTheme( "gametextarea" );
		add( txtLogout );
		
		btnLogout = new Button();
		btnLogout.setTheme( "submitbutton" );
		btnLogout.setText( "Login" );
		add( btnLogout );
		
		btnExit = new Button();
		btnExit.setTheme( "submitbutton" );
		btnExit.setText( "Exit" );
		add( btnExit );
	}
	
	protected void setLayout()
	{
		int x, y, w, h, winW, winH;
		
		winW = Display.getDisplayMode().getWidth();
		winH = Display.getDisplayMode().getHeight();
		
		w = Math.min( 6 * winW / 10, lblTitle.getMaxWidth() );
		h = Math.min( 3 * winH / 32, lblTitle.getMaxHeight() );
		lblTitle.setSize( w, h );
		x = ( winW - lblTitle.getWidth() ) / 2;
		y = 2 * winH / 32;
		lblTitle.setPosition( x, y );
		lblTitle.setAlignment( Alignment.CENTER );
		
		//w = Math.min( 8 * winW / 10, txtLogon.getMaxWidth() );
		//h = Math.min( 8 * winH / 32, txtLogon.getMaxHeight() );
		w = 8 * winW / 10;
		h = 10 * winH / 32;
		txtLogout.setSize( w, h );
		x = ( winW - txtLogout.getWidth() ) / 2;
		y = 6 * winH / 32;
		txtLogout.setPosition( x, y );
		
		//CGuiUtils.println( "w: " + w + ", h: " + h + ", x: " + x + ", y: " + y );
		//CGuiUtils.println( "maxwidth: " + txtLogon.getMaxWidth() + ", maxheight: " + txtLogon.getMaxHeight() );
		
		y = 18 * winH / 32;
		y = 22 * winH / 32;

		y = 18 * winH / 32;
		y = 22 * winH / 32;
		
		w = Math.min( 8 * winW / 30, btnLogout.getMaxWidth() );
		h = Math.min( 3 * winH / 32, btnLogout.getMaxHeight() );
		btnLogout.setSize( w, h );
		btnExit.setSize( w, h );
		
		x = ( winW - ( btnLogout.getWidth() * 3 ) ) / 4;
		y = 27 * winH / 32;
		btnLogout.setPosition( x, y );
		
		x = 3 * ( winW - ( btnExit.getWidth() * 3 ) ) / 4 + ( 2 * btnExit.getWidth() );
		btnExit.setPosition( x, y );
	}
	
	protected void addCallBacks()
	{
		btnLogout.addCallback( new Runnable()
		{
			public void run()
			{
				btnLogout.setText( "Logging in..." );
				btnLogout.setEnabled( false );
				
				txtLogoutModel.setText( "Verifying account credentials..." );
			}
		} );
		
		btnExit.addCallback( new Runnable()
		{
			public void run()
			{
				txtLogoutModel.setText( "Goodbye." );
				System.exit( 0 );
			}
		} );
	}
	
	public void initialize()
	{
		try
		{
			// Start up and initialize LWJGL
			Display.setDisplayMode( new DisplayMode( DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT ) );
			Display.create();
			Display.setTitle( "Beat the Bard GUI ProtoType" );
			Display.setVSyncEnabled( true );
			
			// Instantiate a LWJGLRenderer object
			LWJGLRenderer renderer = new LWJGLRenderer();
			
			// In the original sample code this method was the main method, and the class would be instantiated here.
			// ------
			
			// Create GUI object, pass references to main GUI component and LWJGL renderer. In this step, button widget objects are created
			// and added as children to the main GUI component.
			gui = new GUI( this, renderer );
			
			// Create ThemeManager object, pass path to .xml file containing display instructions to its constructor. Also pass the LWJGL renderer.
			// Then, apply the theme to the GUI object.
			
			/** NOTE: Relative file paths start from "workspace/btb_gui_test/bin/btb_gui" - that is, the folder containing the .class files **/
			/** At least, this is true while developing on Portable Eclipse on G:\" **/
			
			String theme_file_path = "../../resource/graphics/btb_theme_02.xml";
			theme = ThemeManager.createThemeManager( CLoginGui.class.getResource( theme_file_path ), renderer );
			gui.applyTheme( theme );
			
			setLayout();
			addCallBacks();
		}
		catch( Exception ex )
		{
			CGuiUtils.errMsg( ex );
		}
	}
	
	public void invoke()
	{
		// Main loop of the GUI component. gui.update() renders the controls using OpenGL, Display.update() tells LWJGL to update the screen.
		// ReduceInputLag tells LWJGL to process messages and poll the mouse and keyboard.
		try
		{
			if( !Display.isCloseRequested() && !this.quit )
			{
				GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
				
				gui.update();
				Display.update();
				CGuiUtils.reduceInputLag();
			}
		}
		catch( Exception ex )
		{
			CGuiUtils.errMsg(ex);
		}
	}
	
	public void dismiss()
	{
		try
		{
			this.gui.destroy();
			this.theme.destroy();
			Display.destroy();
		}
		catch( Exception ex )
		{
			CGuiUtils.errMsg(ex);
		}
	}
}
